/**
 * Copyright (c) 2008, Andrew Carter All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Andrew Carter nor the names of
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.propertytable;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * @author Carter
 * 
 */
public class PropertyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private ArrayList<PropertySection> sections = new ArrayList<PropertySection>(1);
	
	private ArrayList<ModelSetValueListener> setValueListeners = new ArrayList<ModelSetValueListener>();

	/**
	 * 
	 */
	public PropertyTableModel() {

	}

	/**
	 * 
	 * @param section
	 */
	public void addPropertySection(PropertySection section) {

		sections.add(section);
	}

	/**
	 * 
	 * @param sectionName
	 */
	public void removePropertySection(String sectionName) {
		
		PropertySection section = getPropertySection(sectionName);

		if(section != null) {
			sections.remove(section);
		}
	}

	/**
	 * 
	 * @param section
	 */
	public void removePropertySection(PropertySection section) {
		
		sections.remove(section);
	}

	/**
	 * 
	 * @param section
	 */
	public void addPropertySection(int index, PropertySection section) {

		sections.add(index, section);
	}

	/**
	 * 
	 * @param section
	 * @param name
	 * @return
	 */
	public Object getPropertyObject(String section, String name) {
		
		return getPropertySection(section).getProperty(name).getPropertyObject();
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	public PropertySection getPropertySection(String name) {

		for(PropertySection section : sections) {

			if(section.getSectionName().compareToIgnoreCase(name) == 0)
				return section;
		}

		return null;
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public PropertySection getPropertySection(int index) {

		return sections.get(index);
	}

	/**
	 * 
	 * @return
	 */
	public int getPropertSectionCount() {

		return sections.size();
	}

	/**
	 * 
	 */
	@Override
	public String getColumnName(int col) {

		return "";
	}

	/** 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	public int getColumnCount() {

		return 3;
	}

	/**
	 * Returns the number of visible row in the table.
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	public int getRowCount() {

		int count = 0;

		for(PropertySection section : sections)
			// Plus 1 for the section name
			count += (section.getState() == PropertySectionState.COLLAPSED ? 0 : section.getPropertyCount()) + 1;

		return count;
	}

	/**
	 * 
	 */
	@Override
	public boolean isCellEditable(int row, int column) {

		int count = 0;

		for(PropertySection section : sections) {

			if(count == row) {

				if((column == 0) || (column == 1))
					return true;
				else
					return false;
			}

			// Increment for each section
			count += 1;

			if(section.getState() == PropertySectionState.EXPANDED) {

				if((section.getPropertyCount() + count) > row) {

					if(column == 2)
						return true;
					else
						return false;
				}
				else
					count += section.getPropertyCount();
			}
		}

		return false;
	}

	/** 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	public Object getValueAt(int row, int column) {

		int count = 0;

		for(PropertySection section : sections) {

			if(count == row) {

				if(column == 0)
					return section.getStateObject();
				else if(column == 1)
					return section;

				break;
			}

			// Increment for each section
			count += 1;

			if(section.getState() == PropertySectionState.EXPANDED) {

				if((section.getPropertyCount() + count) > row) {

					Property property = section.getProperty(row - count);

					if(column == 1)
						return property.getPropertyName();
					else if(column == 2)
						return property.getPropertyObject();

					break;
				}
				else
					count += section.getPropertyCount();
			}
		}

		return null;
	}

	/**
	 * @see javax.swing.table.TableModel#setValueAt(Object, int, int)
	 */
	@Override
	public void setValueAt(Object value, int row, int column) {

		if(column != 2)
			return;

		int count = 0;

		for(PropertySection section : sections) {

			if(count == row)
				break;

			// Increment for each section
			count += 1;

			if(section.getState() == PropertySectionState.EXPANDED) {

				if((section.getPropertyCount() + count) > row) {

					Property property = section.getProperty(row - count);
					property.setPropertyObject(value);

					break;
				}
				else
					count += section.getPropertyCount();
			}
		}
		
		fireTableCellUpdated(row, column);
		notifySetValueListeners();
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void addSetValueListener(ModelSetValueListener listener) {
		
		if(!setValueListeners.contains(listener))
			setValueListeners.add(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public void removeSetValueListener(ModelSetValueListener listener) {
		
		if(setValueListeners.contains(listener))
			setValueListeners.remove(listener);
	}
	
	/**
	 * 
	 * @param listener
	 */
	public ModelSetValueListener[] getSetValueListeners() {

		ModelSetValueListener[] template = new ModelSetValueListener[0];
		
		return setValueListeners.toArray(template);
	}
	
	/**
	 * 
	 * @param value
	 */
	private void notifySetValueListeners() {
		
		for(ModelSetValueListener l : setValueListeners)
			l.valueSet();
	}
	
	/**
	 * 
	 * @author Carter
	 *
	 */
	public interface ModelSetValueListener {
		
		public void valueSet();
	}
}
